package org.apache.mina.example.lily.service;

import java.net.InetSocketAddress;

import org.apache.mina.core.future.IoFuture;
import org.apache.mina.core.future.IoFutureListener;
import org.apache.mina.core.future.WriteFuture;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.transport.socket.nio.NioSocketConnector;

public class MyClient {

	NioSocketConnector connector;

	public MyClient() {
		connector = new NioSocketConnector();
		connector.getSessionConfig().setReuseAddress(true);
		connector.getSessionConfig().setTcpNoDelay(true);
		connector.getSessionConfig().setKeepAlive(true);
		connector.getFilterChain().addLast("aaa", new ProtocolCodecFilter(new Codec(true)));
		connector.setHandler(new IoHandlerAdapter() {
			@Override
			public void sessionCreated(IoSession session) throws Exception {
				System.out.println("send message");
				HttpRequestBean bean = new HttpRequestBean();
				bean.setPost("asdsadasdsadasdsa");
				bean.setUrl("asdsadasdsadsadas");
				WriteFuture future = session.write(bean);
				future.addListener(new IoFutureListener<IoFuture>() {
					@Override
					public void operationComplete(IoFuture future) {
						System.out.println("finish");
					}
				});
			}
			
			@Override
			public void messageSent(IoSession session, Object message)
					throws Exception {
				super.messageSent(session, message);
			}
		});
	}
	
	public static void main(String[] args) {
		MyClient client = new MyClient();
		client.connector.connect(new InetSocketAddress("localhost", 80));
	}
}
